﻿using Module.Common.PubEvent;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Module.Common.Common.QiPuHelper
{
    public class ContractTreeOfQiPu : BindableBase
    {
        private readonly IEventAggregator Ea;
        public string ModuleName { get; set; }
        public string FileName { get; set; }
        private TreeNodeOfQiPu _contractTreeQiPu;

        public TreeNodeOfQiPu ConTractTreeQiPu { get { return _contractTreeQiPu; } set { SetProperty(ref _contractTreeQiPu, value); } }
        public ContractTreeOfQiPu(IEventAggregator ea, IContainerProvider containerProvider)
        {
            Ea = ea;
            ConTractTreeQiPu = containerProvider.Resolve<TreeNodeOfQiPu>();
            Ea.GetEvent<ModuleMessageEvent>().Subscribe(MessageProcess);
            Ea.GetEvent<QiPuMessageEvent>().Subscribe(QiPuMessageProcess, ThreadOption.PublisherThread, false, filter => filter.ModuleName.Equals(ModuleName));
        }

        private void QiPuMessageProcess(QiPuMessage obj)
        {
            switch (obj.MessageName)
            {
                case "fullTreeHasUpdate":
                    ConTractTreeQiPu.Childrens.Clear();
                    ObservableCollection<TreeNodeOfQiPu> result = ConvertFormFullToContract(obj.TreeofQipu);
                    foreach (TreeNodeOfQiPu child in result) { ConTractTreeQiPu.Childrens.Add(child); }
                    break;

                default:
                    break;
            }
        }

        private void MessageProcess(ModuleMessage obj)
        {
            switch (obj.MessageName)
            {
                case "reStart": // 重新开局
                    ConTractTreeQiPu.Childrens.Clear();
                    break;
                default:
                    break;
            }
        }

        private ObservableCollection<TreeNodeOfQiPu> ConvertFormFullToContract(TreeNodeOfQiPu fulltree)
        {
            if (fulltree == null) return null;
            ObservableCollection<TreeNodeOfQiPu> conTractTreeOfQiPu = new();

            foreach (TreeNodeOfQiPu curNode in fulltree.Childrens)
            {
                TreeNodeOfQiPu qpstep = new();
                qpstep.CopyFromTreeNode(curNode);
                //qpstep.SetStepData(curNode.QiZiId, curNode.X0, curNode.Y0, curNode.X1, curNode.Y1, curNode.DieQz);
                //qpstep.ID = curNode.ID;
                conTractTreeOfQiPu.Add(qpstep);
                ObservableCollection<TreeNodeOfQiPu> next = ConvertFormFullToContract(curNode);

                if (fulltree.Childrens.Count == 1) // 当前节点如果没有兄弟结点，则子结点设置为兄弟结点
                {
                    foreach (TreeNodeOfQiPu child in next)
                    {
                        conTractTreeOfQiPu.Add(child);
                    }
                }
                else
                {
                    foreach (TreeNodeOfQiPu child in next)
                    {
                        qpstep.Childrens.Add(child); // 当前节点如果有兄弟结点，则子结点仍设置为子结点
                    }
                }
            }
            return conTractTreeOfQiPu;
        }
    }
}
